package com.example.block_chain.service.impl.classs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.block_chain.common.ResponseResult;
import com.example.block_chain.domain.dto.ClassDto;
import com.example.block_chain.domain.pojo.Class;
import com.example.block_chain.domain.pojo.Student;
import com.example.block_chain.mapper.ClassMapper;
import com.example.block_chain.service.classs.ClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public ResponseResult addClass(ClassDto dto) {
        Class aClass = new Class();
        BeanUtils.copyProperties(dto, aClass);

        classMapper.insert(aClass);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteClass(ClassDto dto) {
        Long id = dto.getId();

        classMapper.deleteById(id);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateClass(ClassDto dto) {
        Class aClass = new Class();
        BeanUtils.copyProperties(dto, aClass);

        classMapper.updateById(aClass);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult selectClass(ClassDto dto) {
        String facultyCode = dto.getFacultyCode();
        String schoolCode = dto.getSchoolCode();
        Integer page = dto.getPage();
        Integer pageSize = dto.getPageSize();

        QueryWrapper<Class> queryWrapper = new QueryWrapper();

        List<Class> classs;
        IPage<Class> classIPage = new Page<>(page, pageSize);
        if ((facultyCode == "" || facultyCode == null) && (schoolCode == "" || schoolCode == null)) {
        } else if (facultyCode == "" || facultyCode == null) {
            queryWrapper.eq("school_code", schoolCode);
        } else if (schoolCode == "" || schoolCode == null) {
            queryWrapper.eq("faculty_code", facultyCode);
        } else {
            queryWrapper.eq("school_code", schoolCode).eq("faculty_code", facultyCode);
        }
        classs = classMapper.selectPage(classIPage, queryWrapper).getRecords();

        return ResponseResult.okResult(classs);

    }

    @Override
    public List<Class> getClassInfoByIds(List<String> classIdList) {
        return classMapper.selectBatchIds(classIdList);
    }

    @Override
    public Class getClassInfoByCode(Student student) {
        QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_code", student.getClassCode())
                .eq("school_code", student.getSchoolCode())
                .eq("faculty_code", student.getFacultyCode());
        return classMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Class> selectClass(String facultyCode, String schoolCode) {
        LambdaQueryWrapper<Class> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!facultyCode.isBlank(), Class::getFacultyCode, facultyCode)
                .eq(Class::getSchoolCode, schoolCode);
        return classMapper.selectList(queryWrapper);
    }


}
